#include <GL/glew.h>
#include <FreeImage.h>
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

class objects
{
	public:
		objects();
		~objects();
		void setPosScale(float, float, float , float);
		void setPos(float, float );
		void setScale(float, float );
		void getPos(float &, float &);
		void getScale(float &, float &);
		void printPos();
		void setTexture(char*);
		GLuint bindTexture();
		void translateScale(float &, float &, float&, float&);
		void changeDirX();
		void changeDirY();
		void getDir(int &, int &);
		float getAlpha();
		void setAlpha(float);
		float getRotation();
		void setRotation(float);
		float getRotationZ();
		void setRotationZ(float);
		float getPosZ();
		void setPosZ(float);
		float getLevel();
		void setLevel(float);

private:
		GLuint texture;
		float posX;
		float posY;
		float posZ;
		float scaleX;
		float scaleY;
		int directionX;
		int directionY;
		float alpha;
		float rotation;
		float rotationZ;
};


objects::objects()
{
	posX = 0.0f;
	posY = 0.0f;
	posZ = 1.0f;
	scaleX = 100.0f;
	scaleY = 100.0f;
	directionX = 1;
	directionY = 1;
	alpha = 1.0f;
	rotation = 0.0f;
	rotationZ = 0.0f;
}
objects::~objects()
{

}

void objects::setPosScale(float x, float y, float sx, float sy){
	posX = x;
	posY = y;
	scaleX = sx;
	scaleY = sy;
}
void objects::setPos(float x, float y){
	posX = x;
	posY = y;
}
void objects::setScale(float sx, float sy){
	scaleX = sx;
	scaleY = sy;
}
void objects::changeDirX(){
	directionX *= -1;
}
void objects::changeDirY(){
	directionY *= -1;
}
void objects::getDir(int &x, int &y){
	x = directionX;
	y = directionY;
}
void objects::getPos(float &x, float &y){
	x = posX;
	y  = posY;
}

void objects::getScale(float &x, float &y){
	x = scaleX;
	y  = scaleY;
}
float objects::getAlpha(){
	return alpha;
}
void objects::setAlpha(float a){
	alpha = a;
}
float objects::getRotation(){
	return rotation;
}
void objects::setRotation(float r){
	rotation = r;
}
float objects::getRotationZ(){
	return rotationZ;
}
void objects::setRotationZ(float r){
	rotationZ = r;
}
float objects::getPosZ(){
	return posZ;
}
void objects::setPosZ(float z){
	posZ = z;
}

void objects::printPos(){
	printf("x: %.2f\ty: %.2f\n", posX, posY);
	printf("scalex: %.2f\tscaley: %.2f\n", scaleX, scaleY);
}

void objects::setTexture(char* textureFile){
	glEnable(GL_TEXTURE_2D);
	//Load textures

		// Get the image file type from FreeImage.
		FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(textureFile, 0);

		// Actually load the image file.
		FIBITMAP *dibs = FreeImage_Load(fifmt, textureFile,0);
		dibs = FreeImage_ConvertTo32Bits(dibs);

		if( dibs != NULL ){
					glGenTextures(1, &texture ); 
					glBindTexture(GL_TEXTURE_2D, texture);
	

			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			BYTE *bitss = new BYTE[FreeImage_GetWidth(dibs) * FreeImage_GetHeight(dibs) * 3];
			BYTE *pixelss = (BYTE*)FreeImage_GetBits(dibs);

			glTexImage2D(GL_TEXTURE_2D, 0, 4, FreeImage_GetWidth(dibs), 
			FreeImage_GetHeight(dibs), 0, GL_BGRA, GL_UNSIGNED_BYTE,  pixelss);
			glBindTexture(GL_TEXTURE_2D, 0 );
			FreeImage_Unload(dibs);

			delete bitss;
		}

		else{
			printf("************************************************************\n");
			printf("texture:%s didnt load!!!\n", textureFile);
			printf("************************************************************\n");
		}
}

GLuint objects::bindTexture(){
	return texture;
}

void objects::translateScale(float &x, float &y, float &sx, float &sy){
	x = posX;
	y = posY;
	sx = scaleX;
	sy = scaleY;
}
